<?php


namespace app\index\controller;


use app\model\Member;
use app\model\Order;
use app\model\OrderStaff;
use app\model\Store;
use app\model\StoreBail;
use app\model\Technical;
use app\model\TechnicalBail;

class StorebailController extends Base
{
    public function getBail(){
        $uuid = $this->getuuid();
        $store = Store::where(['uuid'=>$uuid])->find();
        if(empty($store)){
            return $this->json(['data'=>['bail'=>0.00]]);
        }else{
            $store = $store->toArray();
        }
        return $this->json(['data'=>['bail'=>$store['bail']]]);
    }

    public function recharge(){
        $uuid = $this->getuuid();
        $store = Store::where(['uuid'=>$uuid])->find();
        if(empty($store)){
            return $this->json(['data'=>['bail'=>0.00]]);
        }else{
            $store = $store->toArray();
        }

        $bail = StoreBail::create([
            'type'=>1,
            'amount'=>input('post.amount'),
            'sid'=> $store['id']
        ]);
        $bail->toArray();

        return $this->json(['data'=>$bail]);
    }

    public function withdraw(){
        $amount = input('post.amount');

        $uuid = $this->getuuid();
        $store = Store::where(['uuid'=>$uuid])->find();
        if(empty($store)){
            return $this->json(['data'=>['bail'=>0.00]]);
        }else{
            $store = $store->toArray();
        }

        $orders = Order::whereIn('sid',$store['id'])->where('order_status_id','<',5)->select()->toArray();
        if(count($orders)){
            return $this->json(['errno'=>1,'msg'=>'提现失败：有服务订单未完成']);
        }

        if($store['bail'] < $amount) {
            return $this->json(['errno'=>1,'msg'=>'提现失败：保证金余额不足']);
        }

        if(TechnicalBail::addBail([
            'type'=>1,
            'amount'=>$amount,
            'sid'=>  $store['id']
        ])){
            return $this->json(['errno'=>1,'msg'=>'提现失败']);
        }

        return $this->json([]);
    }

    public function bailLog(){
        $uuid = $this->getuuid();
        $store = Store::where(['uuid'=>$uuid])->find();
        if(empty($store)){
            return $this->json(['data'=>['bail'=>0.00]]);
        }else{
            $store = $store->toArray();
        }

        $res = TechnicalBail::where(['sid'=>$store['id']])->select()->toArray();

        return $this->json(['data'=>$res]);
    }
}
